package com.ysd.lis.enums;

import com.ysd.lis.entity.DataBaseBo;
import com.ysd.lis.entity.WkCalcDatabase;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.function.BiFunction;

/**
 * @author LuoXS
 * @date 2024/4/26
 * version 1.0
 */
@Getter
@AllArgsConstructor
public enum DataSourceEnum {

    ORACLE(0L,
            "oracle.jdbc.OracleDriver",
            "jdbc:oracle:thin:@{0}:{1}:{2}", Tools.func),
    SQL_SERVER(1L,
            "com.microsoft.sqlserver.jdbc.SQLServerDriver",
            "jdbc:sqlserver://{0}:{1};DatabaseName={2}", Tools.func),
    DM(2L,
            "dm.jdbc.driver.DmDriver",
            "jdbc:dm://{0}:{1}/{2}?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8", Tools.func),
    none(999L,
            "None",
            "", Tools.func),
    ;


    private final Long dataSourceType;
    private final String driverClass;
    private final String dataSourceUrlFormat;
    private final BiFunction<DataSourceEnum, WkCalcDatabase, DataBaseBo> func;


    public static DataSourceEnum getEnum(WkCalcDatabase calcDatabase) {
        return Arrays.stream(DataSourceEnum.values()).filter(ele -> ele.getDataSourceType().equals(calcDatabase.getDatabaseType())).findFirst().orElse(none);
    }


    public static class Tools {
        public static BiFunction<DataSourceEnum, WkCalcDatabase, DataBaseBo> func = (x, y) ->
                DataBaseBo.builder()
                        .driverClassName(x.getDriverClass())
                        .url(MessageFormat.format(x.getDataSourceUrlFormat(), y.getHost(), y.getPort(), y.getDbName()))
                        .username(y.getUsername())
                        .password(y.getPassword())
                        .build();
    }
}
